package TankGame01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener {
    //定义我的坦克
    MyTank myTank = null;
//    //敌方坦克
//    EnemyTank enemyTank1 = null;
//    EnemyTank enemyTank2 = null;
//    EnemyTank enemyTank3 = null;
    //利用Vector来存放EnemyTank对象
    Vector<EnemyTank> enemyTanks = null;

    public MyPanel() {
        myTank = new MyTank(100, 100);//初始化我的坦克位置位置
        myTank.setSpeed(5);

        enemyTanks = new Vector<>();
        int enemyTankSize = 3;
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank((i + 1) * 100, 0);
            enemyTank.setDirect(2);
            enemyTanks.add(enemyTank);//初始化敌人坦克位置
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);//填充一个游戏背景,默认黑色
        //调用drawTank方法生成坦克对象
        drawTank(myTank.getX(), myTank.getY(), g, myTank.getDirect(), 0);//调用方法设置坦克的图像
//        for (EnemyTank enemyTank :enemyTanks) {
//            drawTank(enemyTank.getX(),enemyTank.getY(),g,2,1);
//        }
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),1);
        }
    }

    /**
     * @param x      坦克的左上角x坐标
     * @param y      坦克的左上角y坐标
     * @param g      画笔
     * @param direct 坦克的方向
     * @param type   坦克的类型(我方坦克 敌方坦克)
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        //根据不同类型的坦克设置不同的颜色
        switch (type) {
            case 0://我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1://敌方坦克
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        //direct表示方向:(0:向上 1:向右 2:向下 3:向左)

        switch (direct) {
            case 0://向上的坦克
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1://向右的坦克
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2://向下的坦克
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3://向左的坦克
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;
            default:
                System.out.println("没有处理...");
        }
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {

    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {
        System.out.println(keyEvent.getKeyChar());
        if (keyEvent.getKeyCode() == keyEvent.VK_W) {
            myTank.setDirect(0);
            myTank.moveUp();
        } else if (keyEvent.getKeyCode() == keyEvent.VK_D) {
            myTank.setDirect(1);
            myTank.moveRight();
        } else if (keyEvent.getKeyCode() == keyEvent.VK_S) {
            myTank.setDirect(2);
            myTank.moveDown();
        } else if (keyEvent.getKeyCode() == keyEvent.VK_A) {
            myTank.setDirect(3);
            myTank.moveLeft();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {

    }
}
